﻿///<reference path="jquery.min.js" />
(function($) {
    jQuery.fn.doubleSelectBox = function(options) {
        var opts = $.extend({}, jQuery.fn.doubleSelectBox.defaults, options);

        opts.boxA = $(opts.listBoxA, this);
        opts.boxB = $(opts.listBoxB, this);

        aToB(opts.boxA, opts.eventName, opts);
        bToA(opts.boxB, opts.eventName, opts);

        if (opts.aToB) {
            $(opts.aToB).each(function(i, item) { aToB(item.target, item.event, opts); });
        }

        if (opts.bToA) {
            $(opts.bToA).each(function(i, item) { bToA(item.target, item.event, opts); });
        }

        return this;
    };

    jQuery.fn.doubleSelectBox.defaults = {
        eventName: 'dblclick',
        listBoxA: 'select:first',
        listBoxB: 'select:eq(1)',
        bToA: false,
        aToB: false
    };   

    jQuery.doubleSelectBox
    {

        var Settings;
        version = function() { alert("1.0") };
        assignRoleFrom = function(from, to) {
            from.children().each(function(i) {
                if (this.selected) {
                    var assignValue = $(this).val();
                    var assignText = $(this).text();
                    from.children("option[value='" + assignValue + "']").remove();
                    to.append("<option value='" + assignValue + "'>" + assignText + "</option>");
                }
            });
        }

        bToA = function(target, eventName, options) {
            $(target).each(function() {
                $(this).bind(eventName, function() { assignRoleFrom(options.boxB, options.boxA); });
            });
        };

        aToB = function(target, eventName, options) {
            $(target).each(function() {
                $(this).bind(eventName, function() {
                    assignRoleFrom(options.boxA, options.boxB)
                });
            });
        }

    }
})($)